package com.closer.server.biz.codebox.service.Impl;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.closer.server.biz.codebox.dto.JudgeContext;
import com.closer.server.biz.codebox.dto.JudgeInfo;
import com.closer.server.biz.codebox.service.JudgeService;
import com.closer.server.biz.problem.dto.JudgeCase;
import com.closer.server.biz.problem.dto.JudgeConfig;
import com.closer.server.mapper.problem.ProblemMapper;
import com.closer.server.mapper.problem.SubmissionMapper;
import com.closer.server.mapper.problem.TestCaseMapper;
import com.closer.server.support.exception.ErrorCode;
import com.closer.server.support.exception.ThrowUtils;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class JudgeServiceImpl implements JudgeService {

    private final SubmissionMapper submissionMapper;

    private final TestCaseMapper testCaseMapper;

    private static final String AUTH_REQUEST_HEADER="codesandbox-auth";

    private static final String AUTH_REQUEST_SECRET="$W$~vrZwe7z&L!ht^U%fF2zZzHTjWSwY%@ZeEJ^*(qZ()D3npx";
    private final ProblemMapper problemMapper;

    public JudgeServiceImpl(SubmissionMapper submissionMapper, TestCaseMapper testCaseMapper, ProblemMapper problemMapper) {
        this.submissionMapper = submissionMapper;
        this.testCaseMapper = testCaseMapper;
        this.problemMapper = problemMapper;
    }
    @Override
    public void doJudge(Long questionSubmitId,Long problemId, String code, String language) {
        log.info("开始判题");
        var updateResult = submissionMapper.updateState(questionSubmitId, "judging");
        ThrowUtils.throwIf(!updateResult, ErrorCode.OPERATION_ERROR,"更新提交记录状态失败");
        var testCase = testCaseMapper.getCases(problemId);
        var ListCases = JSONUtil.toList(testCase, JudgeCase.class);
        try {
            var problemConfig = problemMapper.getJudgeConfig(problemId);
        }catch (Exception e){
            log.error("获取题目配置错误:{ }",e);
        }
        var judgeConfig = JSONUtil.toBean(problemMapper.getJudgeConfig(problemId), JudgeConfig.class);
        var judgeContext = JudgeContext.builder()
                .code(code)
                .language(language)
                .testCases(ListCases)
                .judgeConfig(judgeConfig)
                .build();
        var url = "http://localhost:8090/executeCode";
        var json = JSONUtil.toJsonStr(judgeContext);
        //todo 最后取消注释
        var responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_SECRET)
                .body(json)
                .execute()
                .body();
        ThrowUtils.throwIf(StringUtils.isBlank(responseStr), ErrorCode.API_REQUEST_ERROR,"executeCode remoteSandbox error,message="+responseStr);
        log.info("判题结果:{}",responseStr);
        updateResult = submissionMapper.updateJudgeInfo(questionSubmitId, responseStr);
        ThrowUtils.throwIf(!updateResult, ErrorCode.OPERATION_ERROR,"更新提交记录状态失败");
        updateResult = submissionMapper.updateState(questionSubmitId, "finish");
        ThrowUtils.throwIf(!updateResult, ErrorCode.OPERATION_ERROR,"更新提交记录状态失败");
    }
}
